export const socket = {
	TCPSocket: null,
	receivedStatus: () => {},
	receivedMsgCallBack: () => {},
	receivedHexMsgCallBack: () => {},
	init(ip, port){
		// #ifdef APP-PLUS
		socket.TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin');
		socket.connect(ip, port);
		// #endif
	},
	/**
	 * connect tcp连接
	 */
	connect(ip, port){
		socket.TCPSocket.connect({
			channel: '1',
			ip: ip,
			port: port
		},result => {
			if (result.status == '0') {
				console.log('TCP连接成功');
				// 连接成功注册用户
				if(uni.getStorageSync('token')){
					socket.send(JSON.stringify({
						type:'userRegister',
						token:uni.getStorageSync('token')
					}))
				}
			}
			if(result.status == '1'){
				console.log('TCP断开连接');
			}
			if (result.receivedMsg) {
				uni.$emit('receivedMsgCallBack',result.receivedMsg)
			}
			if (result.receivedHexMsg) {
				socket.receivedHexMsgCallBack(result.receivedHexMsg);
			}
		});
	},
	/**
	 * 客户端 发送字符串消息
	 * @param {Object} channel 通道值
	 * @param {Object} message 发送内容
	 */
	send(message) {
		socket.TCPSocket.send({
			channel: '1', //可选 1~20
			message: message
		});
	},
	/**
	 * 断开连接
	 * @param {Object} channel
	 */
	disconnect(channel) {
		socket.TCPSocket.disconnect({
			channel: '1' //可选 1~20
		});
	}
}